package cn.itcast.nems.config;

import cn.itcast.nems.manager.auth.KeycloakConfig;
import cn.itcast.nems.manager.icc.DictionaryV2Manager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * <p>
 *
 * 字典管理 DictionaryManager  初始化配置
 *
 * @author zhouwei
 * @since 2023/10/16 4:05 PM
 **/
@Configuration
public class DictionaryManagerConfig {
    @Value("${icc.domain}")
    private String domain ;

    /**
     * keycloak api地址
     */
    @Value("${ems2.keycloak.o-api.base-url}")
    private String keyCloakBaseUrl;
    /**
     * keycloak api地址
     */
    @Value("${ems2.keycloak.o-api.realm}")
    private String keyCloakRealm;
    /**
     * keycloak 应用id
     */
    @Value("${ems2.keycloak.o-api.client-id}")
    private String keyCloakClientId;
    /**
     * keycloak 应用秘钥
     */
    @Value("${ems2.keycloak.o-api.cloud-client-id}")
    private String sysClientId;
    @Value("${ems2.keycloak.o-api.client-secret}")
    private String keyCloakClientSecret;

    @Bean
    public DictionaryV2Manager dictionaryV2Manager() {
        String tokenUrl = (keyCloakBaseUrl.endsWith("/") ? keyCloakBaseUrl : keyCloakBaseUrl + "/") +"realms/" + keyCloakRealm + "/protocol/openid-connect/token";
        KeycloakConfig kc = new KeycloakConfig(tokenUrl, keyCloakClientId, keyCloakClientSecret);
        return new DictionaryV2Manager(domain,sysClientId, kc);
    }
}
